#! /bin/sh
# PCP QA Test No. 572
# pmcd agent table relocation and libpcp_pmda botch for PMDA_INTERFACE_3.
# linux PMDA returns no values after weblog Install was the original
# observation.
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -d $PCP_PMDAS_DIR/sample ] || _notrun "sample PMDA directory is not installed"
[ -d $PCP_PMDAS_DIR/simple ] || _notrun "simple PMDA directory is not installed"
[ -d $PCP_PMDAS_DIR/trivial ] || _notrun "trivial PMDA directory is not installed"

pmcdconf=$PCP_PMCDCONF_PATH
rootconf=$PCP_VAR_DIR/pmns/root
needclean=false

unset ROOT TOOLROOT MAKEFLAGS

_cleanup()
{
    if $needclean
    then
	[ -f $tmp.pmcd.bak ] && $sudo cp $tmp.pmcd.bak $pmcdconf
	[ -f $tmp.root.bak ] && $sudo cp $tmp.root.bak $rootconf
	_service pcp restart 2>&1 | _filter_pcp_start
	_wait_for_pmcd
	_wait_for_pmlogger
	$sudo rm -f $tmp.*
	needclean=false
    fi
}

_filter_make()
{
    sed \
	-e '/In function .vpmprintf./d' \
	-e '/warning: the use of .tempnam./d' \
	-e '/make: Nothing to be done/d'
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15
$sudo rm -f $seq.full

# create a basic pmcd config file
if [ $PCP_PLATFORM = linux -o $PCP_PLATFORM = solaris -o $PCP_PLATFORM = freebsd ]
then
cat >$tmp.pmcd.conf << EOF
# from QA $seq
pmcd    2       dso     pmcd_init       $PCP_PMDAS_DIR/pmcd/pmda_pmcd.so
EOF
elif [ $PCP_PLATFORM = darwin ]
then
cat >$tmp.pmcd.conf << EOF
# from QA $seq
pmcd    2       dso     pmcd_init       $PCP_PMDAS_DIR/pmcd/pmda_pmcd.dylib
EOF
else
    echo "Arrgh ... need pmcd.conf for $PCP_PLATFORM"
    exit 1
fi

# create my pmns
cat <<End-of-File >$tmp.pmns
root {
	pmcd
	sample
	trivial
	simple
}
pmcd {
    	numagents	2:0:2
}
sample {
	string
}
sample.string {
	write_me	29:0:32
}
trivial {
	time		250:0:0
}
simple {
	numfetch	253:0:0
}
End-of-File

# real QA test starts here
cp $pmcdconf $tmp.pmcd.bak
cp $rootconf $tmp.root.bak

needclean=true
$sudo cp $tmp.pmcd.conf $pmcdconf 
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | egrep '([p]mcd)|([P]ID)' >>$here/$seq.full
_service pcp restart 2>&1 | _filter_pcp_start
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | egrep '([p]mcd)|([P]ID)' >>$here/$seq.full
_wait_for_pmcd
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | egrep '([p]mcd)|([P]ID)' >>$here/$seq.full
_wait_for_pmlogger

for pmda in '' sample trivial simple
do
    echo
    if [ ! -z "$pmda" ]
    then
	echo "=== $pmda PMDA ==="
	cd $PCP_PMDAS_DIR/$pmda
	$sudo ./Install </dev/null >/dev/null 2> $tmp.stderr
        # strip blank lines from stderr
        # strip cc warning messages (assumed to be 4 non-blanks long)
	cat $tmp.stderr | _filter_make | \
        $PCP_AWK_PROG  '
            /^ *$/ {
                next 
            }
            /^cc-.*cc: WARNING/ {
                getline ; getline ; getline ; getline
                next
            }
            {    
                print
            }
        '
    fi
    pmstore -n $tmp.pmns sample.string.write_me "$pmda"
    pminfo -n $tmp.pmns -f \
	pmcd.numagents \
	sample.string.write_me \
	trivial.time \
	simple.numfetch \
    | $PCP_AWK_PROG '
mask == 1	{ if ($1 == "value") $2 = "NUMBER"
		  mask = 0
		}
		{ print }
/pmcd.numagents/ && "'"$pmda"'" == ""	{ mask = 1 }
/trivial.time$/	{ mask = 1 }'

done

status=0
exit
